import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

void main() {
  test("Duration容错测试", () {
    // 创建一个中心时间为10秒，分别加上和减去1、2秒
    final durationCenter = Duration(seconds: 10);
    final duration1 = durationCenter + Duration(seconds: 1);
    final duration2 = durationCenter + Duration(seconds: 2);
    final duration1Before = durationCenter - Duration(seconds: 1);
    final duration2Before = durationCenter - Duration(seconds: 2);
    // 当让错时间为1.5秒时，duration1测试通过，duration2测试失败
    final delta = Duration(seconds: 1, milliseconds: 500);
    expect(durationCenter.isNear(duration1, tolerance: delta), isTrue);
    expect(durationCenter.isNear(duration1Before, tolerance: delta), isTrue);
    expect(durationCenter.isNear(duration2, tolerance: delta), isFalse);
    expect(durationCenter.isNear(duration2Before, tolerance: delta), isFalse);
  });

  test("DateTime容错测试", () {
    // 创建一个时间，分别加1秒和2秒，也减1、2秒
    final time = DateTime.now();
    final time1 = DateTime.now().add(Duration(seconds: 1));
    final time2 = DateTime.now().add(Duration(seconds: 2));
    final time1Before = DateTime.now().subtract(Duration(seconds: 1));
    final time2Before = DateTime.now().subtract(Duration(seconds: 2));
    // 当容错时间为1.5秒时，time1测试通过，time2失败
    final delta = Duration(seconds: 1, milliseconds: 500);
    expect(time.isNear(time1, tolerance: delta), isTrue);
    expect(time.isNear(time1Before, tolerance: delta), isTrue);
    expect(time.isNear(time2, tolerance: delta), isFalse);
    expect(time.isNear(time2Before, tolerance: delta), isFalse);
  });
}
